{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import keras\n",
    "from keras.datasets import mnist\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Flatten, Activation\n",
    "from keras.layers import Conv2D, MaxPooling2D, BatchNormalization\n",
    "from keras import backend as K\n",
    "\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "batch_size = 2400\n",
    "num_classes = 2\n",
    "epochs = 200"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "train_X = np.load('./train_X.npy')\n",
    "train_Y = np.load('./train_Y.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "test_X = np.load('./test_X.npy')\n",
    "test_Y = np.load('./test_Y.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('float64')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_X.dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(48000, 40, 40, 1)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1000, 40, 40, 1)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = Sequential()\n",
    "model.add(Conv2D(32, kernel_size=(3, 3), activation='relu',input_shape=(40, 40, 1)))\n",
    "model.add(Conv2D(32, (3, 3), strides=1, activation='relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "model.add(Dropout(0.2))\n",
    "\n",
    "model.add(Conv2D(64, (3, 3), strides=1, activation='relu'))\n",
    "model.add(MaxPooling2D(pool_size=(2, 2)))\n",
    "model.add(Dropout(0.4))\n",
    "\n",
    "model.add(Flatten())\n",
    "model.add(Dense(128, activation='relu'))\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(num_classes))\n",
    "model.add(Activation('softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "model.compile(loss=keras.losses.categorical_crossentropy,\n",
    "              optimizer=keras.optimizers.Adadelta(),\n",
    "              metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 48000 samples, validate on 1000 samples\n",
      "Epoch 1/200\n",
      "48000/48000 [==============================] - 385s - loss: 0.6927 - acc: 0.5508 - val_loss: 0.6470 - val_acc: 0.6920\n",
      "Epoch 2/200\n",
      "48000/48000 [==============================] - 503s - loss: 0.6404 - acc: 0.6337 - val_loss: 0.5783 - val_acc: 0.7180\n",
      "Epoch 3/200\n",
      "48000/48000 [==============================] - 453s - loss: 0.5845 - acc: 0.6960 - val_loss: 0.5441 - val_acc: 0.7360\n",
      "Epoch 4/200\n",
      "48000/48000 [==============================] - 422s - loss: 0.5517 - acc: 0.7243 - val_loss: 0.5011 - val_acc: 0.7690\n",
      "Epoch 5/200\n",
      "48000/48000 [==============================] - 389s - loss: 0.5256 - acc: 0.7403 - val_loss: 0.5087 - val_acc: 0.7600\n",
      "Epoch 6/200\n",
      "48000/48000 [==============================] - 526s - loss: 0.5064 - acc: 0.7508 - val_loss: 0.4869 - val_acc: 0.7710\n",
      "Epoch 7/200\n",
      "48000/48000 [==============================] - 628s - loss: 0.4900 - acc: 0.7604 - val_loss: 0.4785 - val_acc: 0.7740\n",
      "Epoch 8/200\n",
      "48000/48000 [==============================] - 566s - loss: 0.4696 - acc: 0.7725 - val_loss: 0.4573 - val_acc: 0.7800\n",
      "Epoch 9/200\n",
      "48000/48000 [==============================] - 427s - loss: 0.4594 - acc: 0.7816 - val_loss: 0.4252 - val_acc: 0.8150\n",
      "Epoch 10/200\n",
      "48000/48000 [==============================] - 427s - loss: 0.4433 - acc: 0.7920 - val_loss: 0.4098 - val_acc: 0.8340\n",
      "Epoch 11/200\n",
      "48000/48000 [==============================] - 416s - loss: 0.4196 - acc: 0.8063 - val_loss: 0.4869 - val_acc: 0.7440\n",
      "Epoch 12/200\n",
      "48000/48000 [==============================] - 407s - loss: 0.4109 - acc: 0.8094 - val_loss: 0.3855 - val_acc: 0.8280\n",
      "Epoch 13/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.4107 - acc: 0.8102 - val_loss: 0.4034 - val_acc: 0.8300\n",
      "Epoch 14/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.3917 - acc: 0.8233 - val_loss: 0.3944 - val_acc: 0.8260\n",
      "Epoch 15/200\n",
      "48000/48000 [==============================] - 403s - loss: 0.3691 - acc: 0.8336 - val_loss: 0.3844 - val_acc: 0.8300\n",
      "Epoch 16/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.3643 - acc: 0.8373 - val_loss: 0.3561 - val_acc: 0.8510\n",
      "Epoch 17/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.3509 - acc: 0.8443 - val_loss: 0.3027 - val_acc: 0.8760\n",
      "Epoch 18/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.3410 - acc: 0.8488 - val_loss: 0.3089 - val_acc: 0.8740\n",
      "Epoch 19/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.3293 - acc: 0.8552 - val_loss: 0.3184 - val_acc: 0.8640\n",
      "Epoch 20/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.3110 - acc: 0.8659 - val_loss: 0.3693 - val_acc: 0.8280\n",
      "Epoch 21/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.3063 - acc: 0.8681 - val_loss: 0.2350 - val_acc: 0.9030\n",
      "Epoch 22/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.2972 - acc: 0.8734 - val_loss: 0.3153 - val_acc: 0.8830\n",
      "Epoch 23/200\n",
      "48000/48000 [==============================] - 406s - loss: 0.2773 - acc: 0.8832 - val_loss: 0.3457 - val_acc: 0.8410\n",
      "Epoch 24/200\n",
      "48000/48000 [==============================] - 407s - loss: 0.2636 - acc: 0.8908 - val_loss: 0.1952 - val_acc: 0.9400\n",
      "Epoch 25/200\n",
      "48000/48000 [==============================] - 411s - loss: 0.2687 - acc: 0.8864 - val_loss: 0.2378 - val_acc: 0.9170\n",
      "Epoch 26/200\n",
      "48000/48000 [==============================] - 407s - loss: 0.2389 - acc: 0.9051 - val_loss: 0.2115 - val_acc: 0.9140\n",
      "Epoch 27/200\n",
      "48000/48000 [==============================] - 405s - loss: 0.2386 - acc: 0.8994 - val_loss: 0.1878 - val_acc: 0.9310\n",
      "Epoch 28/200\n",
      "48000/48000 [==============================] - 414s - loss: 0.2308 - acc: 0.9042 - val_loss: 0.2186 - val_acc: 0.9120\n",
      "Epoch 29/200\n",
      "48000/48000 [==============================] - 408s - loss: 0.2211 - acc: 0.9093 - val_loss: 0.1741 - val_acc: 0.9420\n",
      "Epoch 30/200\n",
      "48000/48000 [==============================] - 415s - loss: 0.2109 - acc: 0.9141 - val_loss: 0.2196 - val_acc: 0.9070\n",
      "Epoch 31/200\n",
      "48000/48000 [==============================] - 408s - loss: 0.2128 - acc: 0.9131 - val_loss: 0.1474 - val_acc: 0.9530\n",
      "Epoch 32/200\n",
      "48000/48000 [==============================] - 417s - loss: 0.1923 - acc: 0.9241 - val_loss: 0.2026 - val_acc: 0.9190\n",
      "Epoch 33/200\n",
      "48000/48000 [==============================] - 408s - loss: 0.2005 - acc: 0.9192 - val_loss: 0.1462 - val_acc: 0.9470\n",
      "Epoch 34/200\n",
      "48000/48000 [==============================] - 415s - loss: 0.1778 - acc: 0.9296 - val_loss: 0.1359 - val_acc: 0.9430\n",
      "Epoch 35/200\n",
      "48000/48000 [==============================] - 418s - loss: 0.1694 - acc: 0.9319 - val_loss: 0.1442 - val_acc: 0.9410\n",
      "Epoch 36/200\n",
      "48000/48000 [==============================] - 420s - loss: 0.1611 - acc: 0.9356 - val_loss: 0.1410 - val_acc: 0.9450\n",
      "Epoch 37/200\n",
      "48000/48000 [==============================] - 416s - loss: 0.1744 - acc: 0.9308 - val_loss: 0.1505 - val_acc: 0.9390\n",
      "Epoch 38/200\n",
      "48000/48000 [==============================] - 423s - loss: 0.1508 - acc: 0.9408 - val_loss: 0.1313 - val_acc: 0.9490\n",
      "Epoch 39/200\n",
      "48000/48000 [==============================] - 421s - loss: 0.1532 - acc: 0.9402 - val_loss: 0.1030 - val_acc: 0.9580\n",
      "Epoch 40/200\n",
      "48000/48000 [==============================] - 420s - loss: 0.1511 - acc: 0.9405 - val_loss: 0.0940 - val_acc: 0.9600\n",
      "Epoch 41/200\n",
      "48000/48000 [==============================] - 411s - loss: 0.1411 - acc: 0.9446 - val_loss: 0.0939 - val_acc: 0.9670\n",
      "Epoch 42/200\n",
      "48000/48000 [==============================] - 413s - loss: 0.1323 - acc: 0.9498 - val_loss: 0.0847 - val_acc: 0.9690\n",
      "Epoch 43/200\n",
      "48000/48000 [==============================] - 414s - loss: 0.1258 - acc: 0.9510 - val_loss: 0.0928 - val_acc: 0.9660\n",
      "Epoch 44/200\n",
      "48000/48000 [==============================] - 422s - loss: 0.1029 - acc: 0.9606 - val_loss: 0.0684 - val_acc: 0.9720\n",
      "Epoch 45/200\n",
      "48000/48000 [==============================] - 436s - loss: 0.1176 - acc: 0.9545 - val_loss: 0.0708 - val_acc: 0.9750\n",
      "Epoch 46/200\n",
      "48000/48000 [==============================] - 417s - loss: 0.1167 - acc: 0.9548 - val_loss: 0.0700 - val_acc: 0.9760\n",
      "Epoch 47/200\n",
      "48000/48000 [==============================] - 430s - loss: 0.1010 - acc: 0.9606 - val_loss: 0.0808 - val_acc: 0.9680\n",
      "Epoch 48/200\n",
      "48000/48000 [==============================] - 416s - loss: 0.1029 - acc: 0.9605 - val_loss: 0.0634 - val_acc: 0.9750\n",
      "Epoch 49/200\n",
      "48000/48000 [==============================] - 417s - loss: 0.1041 - acc: 0.9596 - val_loss: 0.0685 - val_acc: 0.9790\n",
      "Epoch 50/200\n",
      "48000/48000 [==============================] - 409s - loss: 0.0877 - acc: 0.9672 - val_loss: 0.0643 - val_acc: 0.9750\n",
      "Epoch 51/200\n",
      "48000/48000 [==============================] - 411s - loss: 0.1009 - acc: 0.9622 - val_loss: 0.0699 - val_acc: 0.9720\n",
      "Epoch 52/200\n",
      "48000/48000 [==============================] - 409s - loss: 0.0951 - acc: 0.9641 - val_loss: 0.0860 - val_acc: 0.9660\n",
      "Epoch 53/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.0922 - acc: 0.9654 - val_loss: 0.0537 - val_acc: 0.9830\n",
      "Epoch 54/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.0786 - acc: 0.9709 - val_loss: 0.0570 - val_acc: 0.9820\n",
      "Epoch 55/200\n",
      "48000/48000 [==============================] - 383s - loss: 0.0860 - acc: 0.9673 - val_loss: 0.0501 - val_acc: 0.9820\n",
      "Epoch 56/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0864 - acc: 0.9664 - val_loss: 0.0540 - val_acc: 0.9800\n",
      "Epoch 57/200\n",
      "48000/48000 [==============================] - 408s - loss: 0.0761 - acc: 0.9713 - val_loss: 0.0494 - val_acc: 0.9830\n",
      "Epoch 58/200\n",
      "48000/48000 [==============================] - 393s - loss: 0.0845 - acc: 0.9681 - val_loss: 0.0621 - val_acc: 0.9750\n",
      "Epoch 59/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0792 - acc: 0.9690 - val_loss: 0.0597 - val_acc: 0.9770\n",
      "Epoch 60/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.0730 - acc: 0.9729 - val_loss: 0.0447 - val_acc: 0.9840\n",
      "Epoch 61/200\n",
      "48000/48000 [==============================] - 386s - loss: 0.0798 - acc: 0.9696 - val_loss: 0.0473 - val_acc: 0.9870\n",
      "Epoch 62/200\n",
      "48000/48000 [==============================] - 393s - loss: 0.0656 - acc: 0.9756 - val_loss: 0.0467 - val_acc: 0.9850\n",
      "Epoch 63/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0719 - acc: 0.9724 - val_loss: 0.0490 - val_acc: 0.9830\n",
      "Epoch 64/200\n",
      "48000/48000 [==============================] - 376s - loss: 0.0669 - acc: 0.9756 - val_loss: 0.0424 - val_acc: 0.9870\n",
      "Epoch 65/200\n",
      "48000/48000 [==============================] - 388s - loss: 0.0604 - acc: 0.9779 - val_loss: 0.0415 - val_acc: 0.9860\n",
      "Epoch 66/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0723 - acc: 0.9728 - val_loss: 0.0447 - val_acc: 0.9860\n",
      "Epoch 67/200\n",
      "48000/48000 [==============================] - 413s - loss: 0.0658 - acc: 0.9753 - val_loss: 0.0393 - val_acc: 0.9880\n",
      "Epoch 68/200\n",
      "48000/48000 [==============================] - 416s - loss: 0.0662 - acc: 0.9756 - val_loss: 0.0438 - val_acc: 0.9850\n",
      "Epoch 69/200\n",
      "48000/48000 [==============================] - 405s - loss: 0.0588 - acc: 0.9783 - val_loss: 0.0371 - val_acc: 0.9850\n",
      "Epoch 70/200\n",
      "48000/48000 [==============================] - 403s - loss: 0.0566 - acc: 0.9796 - val_loss: 0.0426 - val_acc: 0.9860\n",
      "Epoch 71/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.0635 - acc: 0.9770 - val_loss: 0.0361 - val_acc: 0.9850\n",
      "Epoch 72/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.0604 - acc: 0.9777 - val_loss: 0.0364 - val_acc: 0.9870\n",
      "Epoch 73/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0543 - acc: 0.9800 - val_loss: 0.0356 - val_acc: 0.9870\n",
      "Epoch 74/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0561 - acc: 0.9789 - val_loss: 0.0363 - val_acc: 0.9870\n",
      "Epoch 75/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0516 - acc: 0.9810 - val_loss: 0.0364 - val_acc: 0.9870\n",
      "Epoch 76/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0551 - acc: 0.9795 - val_loss: 0.0374 - val_acc: 0.9840\n",
      "Epoch 77/200\n",
      "48000/48000 [==============================] - 389s - loss: 0.0568 - acc: 0.9792 - val_loss: 0.0418 - val_acc: 0.9860\n",
      "Epoch 78/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0556 - acc: 0.9791 - val_loss: 0.0336 - val_acc: 0.9900\n",
      "Epoch 79/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0556 - acc: 0.9795 - val_loss: 0.0329 - val_acc: 0.9880\n",
      "Epoch 80/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0483 - acc: 0.9827 - val_loss: 0.0366 - val_acc: 0.9840\n",
      "Epoch 81/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.0572 - acc: 0.9791 - val_loss: 0.0326 - val_acc: 0.9890\n",
      "Epoch 82/200\n",
      "48000/48000 [==============================] - 394s - loss: 0.0461 - acc: 0.9830 - val_loss: 0.0304 - val_acc: 0.9910\n",
      "Epoch 83/200\n",
      "48000/48000 [==============================] - 405s - loss: 0.0467 - acc: 0.9825 - val_loss: 0.0335 - val_acc: 0.9890\n",
      "Epoch 84/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.0498 - acc: 0.9813 - val_loss: 0.0355 - val_acc: 0.9840\n",
      "Epoch 85/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0446 - acc: 0.9837 - val_loss: 0.0318 - val_acc: 0.9900\n",
      "Epoch 86/200\n",
      "48000/48000 [==============================] - 404s - loss: 0.0542 - acc: 0.9794 - val_loss: 0.0323 - val_acc: 0.9900\n",
      "Epoch 87/200\n",
      "48000/48000 [==============================] - 411s - loss: 0.0428 - acc: 0.9841 - val_loss: 0.0305 - val_acc: 0.9890\n",
      "Epoch 88/200\n",
      "48000/48000 [==============================] - 412s - loss: 0.0462 - acc: 0.9832 - val_loss: 0.0298 - val_acc: 0.9910\n",
      "Epoch 89/200\n",
      "48000/48000 [==============================] - 412s - loss: 0.0414 - acc: 0.9840 - val_loss: 0.0368 - val_acc: 0.9860\n",
      "Epoch 90/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0463 - acc: 0.9826 - val_loss: 0.0309 - val_acc: 0.9900\n",
      "Epoch 91/200\n",
      "48000/48000 [==============================] - 402s - loss: 0.0477 - acc: 0.9813 - val_loss: 0.0360 - val_acc: 0.9840\n",
      "Epoch 92/200\n",
      "48000/48000 [==============================] - 416s - loss: 0.0447 - acc: 0.9837 - val_loss: 0.0425 - val_acc: 0.9850\n",
      "Epoch 93/200\n",
      "48000/48000 [==============================] - 414s - loss: 0.0415 - acc: 0.9851 - val_loss: 0.0281 - val_acc: 0.9930\n",
      "Epoch 94/200\n",
      "48000/48000 [==============================] - 416s - loss: 0.0412 - acc: 0.9847 - val_loss: 0.0301 - val_acc: 0.9900\n",
      "Epoch 95/200\n",
      "48000/48000 [==============================] - 404s - loss: 0.0441 - acc: 0.9844 - val_loss: 0.0281 - val_acc: 0.9920\n",
      "Epoch 96/200\n",
      "48000/48000 [==============================] - 409s - loss: 0.0460 - acc: 0.9829 - val_loss: 0.0300 - val_acc: 0.9890\n",
      "Epoch 97/200\n",
      "48000/48000 [==============================] - 432s - loss: 0.0386 - acc: 0.9860 - val_loss: 0.0281 - val_acc: 0.9930\n",
      "Epoch 98/200\n",
      "48000/48000 [==============================] - 425s - loss: 0.0375 - acc: 0.9863 - val_loss: 0.0276 - val_acc: 0.9920\n",
      "Epoch 99/200\n",
      "48000/48000 [==============================] - 434s - loss: 0.0393 - acc: 0.9857 - val_loss: 0.0281 - val_acc: 0.9920\n",
      "Epoch 100/200\n",
      "48000/48000 [==============================] - 440s - loss: 0.0382 - acc: 0.9862 - val_loss: 0.0266 - val_acc: 0.9930\n",
      "Epoch 101/200\n",
      "48000/48000 [==============================] - 431s - loss: 0.0382 - acc: 0.9858 - val_loss: 0.0276 - val_acc: 0.9930\n",
      "Epoch 102/200\n",
      "48000/48000 [==============================] - 428s - loss: 0.0351 - acc: 0.9865 - val_loss: 0.0272 - val_acc: 0.9920\n",
      "Epoch 103/200\n",
      "48000/48000 [==============================] - 428s - loss: 0.0384 - acc: 0.9862 - val_loss: 0.0271 - val_acc: 0.9930\n",
      "Epoch 104/200\n",
      "48000/48000 [==============================] - 434s - loss: 0.0370 - acc: 0.9864 - val_loss: 0.0280 - val_acc: 0.9920\n",
      "Epoch 105/200\n",
      "48000/48000 [==============================] - 434s - loss: 0.0378 - acc: 0.9861 - val_loss: 0.0347 - val_acc: 0.9870\n",
      "Epoch 106/200\n",
      "48000/48000 [==============================] - 426s - loss: 0.0413 - acc: 0.9846 - val_loss: 0.0284 - val_acc: 0.9910\n",
      "Epoch 107/200\n",
      "48000/48000 [==============================] - 434s - loss: 0.0358 - acc: 0.9871 - val_loss: 0.0267 - val_acc: 0.9930\n",
      "Epoch 108/200\n",
      "48000/48000 [==============================] - 435s - loss: 0.0311 - acc: 0.9887 - val_loss: 0.0261 - val_acc: 0.9920\n",
      "Epoch 109/200\n",
      "48000/48000 [==============================] - 411s - loss: 0.0375 - acc: 0.9864 - val_loss: 0.0321 - val_acc: 0.9880\n",
      "Epoch 110/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0395 - acc: 0.9851 - val_loss: 0.0286 - val_acc: 0.9900\n",
      "Epoch 111/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0334 - acc: 0.9875 - val_loss: 0.0259 - val_acc: 0.9940\n",
      "Epoch 112/200\n",
      "48000/48000 [==============================] - 393s - loss: 0.0332 - acc: 0.9874 - val_loss: 0.0325 - val_acc: 0.9870\n",
      "Epoch 113/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.0435 - acc: 0.9834 - val_loss: 0.0305 - val_acc: 0.9880\n",
      "Epoch 114/200\n",
      "48000/48000 [==============================] - 389s - loss: 0.0309 - acc: 0.9891 - val_loss: 0.0252 - val_acc: 0.9920\n",
      "Epoch 115/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0296 - acc: 0.9892 - val_loss: 0.0255 - val_acc: 0.9940\n",
      "Epoch 116/200\n",
      "48000/48000 [==============================] - 400s - loss: 0.0330 - acc: 0.9883 - val_loss: 0.0242 - val_acc: 0.9940\n",
      "Epoch 117/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0322 - acc: 0.9884 - val_loss: 0.0255 - val_acc: 0.9930\n",
      "Epoch 118/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0295 - acc: 0.9892 - val_loss: 0.0254 - val_acc: 0.9900\n",
      "Epoch 119/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0316 - acc: 0.9888 - val_loss: 0.0252 - val_acc: 0.9930\n",
      "Epoch 120/200\n",
      "48000/48000 [==============================] - 406s - loss: 0.0306 - acc: 0.9888 - val_loss: 0.0314 - val_acc: 0.9870\n",
      "Epoch 121/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.0310 - acc: 0.9885 - val_loss: 0.0239 - val_acc: 0.9930\n",
      "Epoch 122/200\n",
      "48000/48000 [==============================] - 409s - loss: 0.0282 - acc: 0.9900 - val_loss: 0.0235 - val_acc: 0.9930\n",
      "Epoch 123/200\n",
      "48000/48000 [==============================] - 408s - loss: 0.0323 - acc: 0.9882 - val_loss: 0.0347 - val_acc: 0.9840\n",
      "Epoch 124/200\n",
      "48000/48000 [==============================] - 415s - loss: 0.0346 - acc: 0.9872 - val_loss: 0.0237 - val_acc: 0.9930\n",
      "Epoch 125/200\n",
      "48000/48000 [==============================] - 413s - loss: 0.0260 - acc: 0.9906 - val_loss: 0.0299 - val_acc: 0.9870\n",
      "Epoch 126/200\n",
      "48000/48000 [==============================] - 418s - loss: 0.0276 - acc: 0.9895 - val_loss: 0.0223 - val_acc: 0.9940\n",
      "Epoch 127/200\n",
      "48000/48000 [==============================] - 411s - loss: 0.0295 - acc: 0.9891 - val_loss: 0.0228 - val_acc: 0.9940\n",
      "Epoch 128/200\n",
      "48000/48000 [==============================] - 407s - loss: 0.0287 - acc: 0.9891 - val_loss: 0.0248 - val_acc: 0.9910\n",
      "Epoch 129/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0269 - acc: 0.9901 - val_loss: 0.0242 - val_acc: 0.9910\n",
      "Epoch 130/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0269 - acc: 0.9901 - val_loss: 0.0231 - val_acc: 0.9940\n",
      "Epoch 131/200\n",
      "48000/48000 [==============================] - 413s - loss: 0.0293 - acc: 0.9893 - val_loss: 0.0236 - val_acc: 0.9930\n",
      "Epoch 132/200\n",
      "48000/48000 [==============================] - 404s - loss: 0.0255 - acc: 0.9909 - val_loss: 0.0229 - val_acc: 0.9920\n",
      "Epoch 133/200\n",
      "48000/48000 [==============================] - 388s - loss: 0.0289 - acc: 0.9891 - val_loss: 0.0281 - val_acc: 0.9900\n",
      "Epoch 134/200\n",
      "48000/48000 [==============================] - 386s - loss: 0.0264 - acc: 0.9906 - val_loss: 0.0232 - val_acc: 0.9930\n",
      "Epoch 135/200\n",
      "48000/48000 [==============================] - 373s - loss: 0.0236 - acc: 0.9914 - val_loss: 0.0240 - val_acc: 0.9910\n",
      "Epoch 136/200\n",
      "48000/48000 [==============================] - 393s - loss: 0.0274 - acc: 0.9897 - val_loss: 0.0223 - val_acc: 0.9920\n",
      "Epoch 137/200\n",
      "48000/48000 [==============================] - 386s - loss: 0.0264 - acc: 0.9903 - val_loss: 0.0248 - val_acc: 0.9910\n",
      "Epoch 138/200\n",
      "48000/48000 [==============================] - 378s - loss: 0.0233 - acc: 0.9921 - val_loss: 0.0233 - val_acc: 0.9940\n",
      "Epoch 139/200\n",
      "48000/48000 [==============================] - 381s - loss: 0.0310 - acc: 0.9888 - val_loss: 0.0233 - val_acc: 0.9940\n",
      "Epoch 140/200\n",
      "48000/48000 [==============================] - 372s - loss: 0.0244 - acc: 0.9912 - val_loss: 0.0237 - val_acc: 0.9930\n",
      "Epoch 141/200\n",
      "48000/48000 [==============================] - 379s - loss: 0.0216 - acc: 0.9923 - val_loss: 0.0228 - val_acc: 0.9930\n",
      "Epoch 142/200\n",
      "48000/48000 [==============================] - 366s - loss: 0.0231 - acc: 0.9912 - val_loss: 0.0233 - val_acc: 0.9940\n",
      "Epoch 143/200\n",
      "48000/48000 [==============================] - 367s - loss: 0.0246 - acc: 0.9915 - val_loss: 0.0240 - val_acc: 0.9930\n",
      "Epoch 144/200\n",
      "48000/48000 [==============================] - 381s - loss: 0.0267 - acc: 0.9910 - val_loss: 0.0225 - val_acc: 0.9940\n",
      "Epoch 145/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0228 - acc: 0.9918 - val_loss: 0.0221 - val_acc: 0.9940\n",
      "Epoch 146/200\n",
      "48000/48000 [==============================] - 375s - loss: 0.0205 - acc: 0.9927 - val_loss: 0.0223 - val_acc: 0.9930\n",
      "Epoch 147/200\n",
      "48000/48000 [==============================] - 383s - loss: 0.0219 - acc: 0.9921 - val_loss: 0.0221 - val_acc: 0.9940\n",
      "Epoch 148/200\n",
      "48000/48000 [==============================] - 389s - loss: 0.0230 - acc: 0.9916 - val_loss: 0.0211 - val_acc: 0.9950\n",
      "Epoch 149/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0223 - acc: 0.9920 - val_loss: 0.0230 - val_acc: 0.9930\n",
      "Epoch 150/200\n",
      "48000/48000 [==============================] - 390s - loss: 0.0216 - acc: 0.9924 - val_loss: 0.0241 - val_acc: 0.9930\n",
      "Epoch 151/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0217 - acc: 0.9925 - val_loss: 0.0224 - val_acc: 0.9940\n",
      "Epoch 152/200\n",
      "48000/48000 [==============================] - 385s - loss: 0.0231 - acc: 0.9911 - val_loss: 0.0235 - val_acc: 0.9940\n",
      "Epoch 153/200\n",
      "48000/48000 [==============================] - 380s - loss: 0.0234 - acc: 0.9914 - val_loss: 0.0219 - val_acc: 0.9940\n",
      "Epoch 154/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0208 - acc: 0.9926 - val_loss: 0.0237 - val_acc: 0.9900\n",
      "Epoch 155/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0214 - acc: 0.9925 - val_loss: 0.0240 - val_acc: 0.9930\n",
      "Epoch 156/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0215 - acc: 0.9923 - val_loss: 0.0226 - val_acc: 0.9940\n",
      "Epoch 157/200\n",
      "48000/48000 [==============================] - 391s - loss: 0.0246 - acc: 0.9911 - val_loss: 0.0239 - val_acc: 0.9940\n",
      "Epoch 158/200\n",
      "48000/48000 [==============================] - 385s - loss: 0.0228 - acc: 0.9915 - val_loss: 0.0250 - val_acc: 0.9930\n",
      "Epoch 159/200\n",
      "48000/48000 [==============================] - 389s - loss: 0.0211 - acc: 0.9925 - val_loss: 0.0224 - val_acc: 0.9940\n",
      "Epoch 160/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0200 - acc: 0.9928 - val_loss: 0.0222 - val_acc: 0.9940\n",
      "Epoch 161/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.0191 - acc: 0.9936 - val_loss: 0.0229 - val_acc: 0.9930\n",
      "Epoch 162/200\n",
      "48000/48000 [==============================] - 385s - loss: 0.0187 - acc: 0.9932 - val_loss: 0.0234 - val_acc: 0.9930\n",
      "Epoch 163/200\n",
      "48000/48000 [==============================] - 383s - loss: 0.0264 - acc: 0.9904 - val_loss: 0.0221 - val_acc: 0.9940\n",
      "Epoch 164/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0182 - acc: 0.9940 - val_loss: 0.0217 - val_acc: 0.9940\n",
      "Epoch 165/200\n",
      "48000/48000 [==============================] - 407s - loss: 0.0179 - acc: 0.9934 - val_loss: 0.0218 - val_acc: 0.9940\n",
      "Epoch 166/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.0206 - acc: 0.9930 - val_loss: 0.0232 - val_acc: 0.9920\n",
      "Epoch 167/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0170 - acc: 0.9937 - val_loss: 0.0254 - val_acc: 0.9910\n",
      "Epoch 168/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0175 - acc: 0.9939 - val_loss: 0.0229 - val_acc: 0.9930\n",
      "Epoch 169/200\n",
      "48000/48000 [==============================] - 403s - loss: 0.0174 - acc: 0.9937 - val_loss: 0.0232 - val_acc: 0.9920\n",
      "Epoch 170/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0189 - acc: 0.9932 - val_loss: 0.0228 - val_acc: 0.9930\n",
      "Epoch 171/200\n",
      "48000/48000 [==============================] - 398s - loss: 0.0210 - acc: 0.9925 - val_loss: 0.0246 - val_acc: 0.9900\n",
      "Epoch 172/200\n",
      "48000/48000 [==============================] - 390s - loss: 0.0168 - acc: 0.9940 - val_loss: 0.0221 - val_acc: 0.9940\n",
      "Epoch 173/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0180 - acc: 0.9937 - val_loss: 0.0220 - val_acc: 0.9940\n",
      "Epoch 174/200\n",
      "48000/48000 [==============================] - 399s - loss: 0.0169 - acc: 0.9940 - val_loss: 0.0232 - val_acc: 0.9910\n",
      "Epoch 175/200\n",
      "48000/48000 [==============================] - 389s - loss: 0.0207 - acc: 0.9924 - val_loss: 0.0222 - val_acc: 0.9940\n",
      "Epoch 176/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0164 - acc: 0.9942 - val_loss: 0.0230 - val_acc: 0.9930\n",
      "Epoch 177/200\n",
      "48000/48000 [==============================] - 405s - loss: 0.0178 - acc: 0.9937 - val_loss: 0.0216 - val_acc: 0.9920\n",
      "Epoch 178/200\n",
      "48000/48000 [==============================] - 403s - loss: 0.0189 - acc: 0.9932 - val_loss: 0.0304 - val_acc: 0.9880\n",
      "Epoch 179/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.0217 - acc: 0.9921 - val_loss: 0.0219 - val_acc: 0.9940\n",
      "Epoch 180/200\n",
      "48000/48000 [==============================] - 388s - loss: 0.0159 - acc: 0.9945 - val_loss: 0.0217 - val_acc: 0.9940\n",
      "Epoch 181/200\n",
      "48000/48000 [==============================] - 390s - loss: 0.0169 - acc: 0.9941 - val_loss: 0.0218 - val_acc: 0.9920\n",
      "Epoch 182/200\n",
      "48000/48000 [==============================] - 406s - loss: 0.0166 - acc: 0.9942 - val_loss: 0.0216 - val_acc: 0.9930\n",
      "Epoch 183/200\n",
      "48000/48000 [==============================] - 404s - loss: 0.0151 - acc: 0.9945 - val_loss: 0.0221 - val_acc: 0.9940\n",
      "Epoch 184/200\n",
      "48000/48000 [==============================] - 396s - loss: 0.0149 - acc: 0.9947 - val_loss: 0.0222 - val_acc: 0.9930\n",
      "Epoch 185/200\n",
      "48000/48000 [==============================] - 393s - loss: 0.0171 - acc: 0.9942 - val_loss: 0.0210 - val_acc: 0.9940\n",
      "Epoch 186/200\n",
      "48000/48000 [==============================] - 404s - loss: 0.0174 - acc: 0.9940 - val_loss: 0.0241 - val_acc: 0.9910\n",
      "Epoch 187/200\n",
      "48000/48000 [==============================] - 397s - loss: 0.0169 - acc: 0.9940 - val_loss: 0.0241 - val_acc: 0.9910\n",
      "Epoch 188/200\n",
      "48000/48000 [==============================] - 393s - loss: 0.0160 - acc: 0.9943 - val_loss: 0.0232 - val_acc: 0.9930\n",
      "Epoch 189/200\n",
      "48000/48000 [==============================] - 395s - loss: 0.0189 - acc: 0.9935 - val_loss: 0.0207 - val_acc: 0.9940\n",
      "Epoch 190/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0150 - acc: 0.9946 - val_loss: 0.0212 - val_acc: 0.9940\n",
      "Epoch 191/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.0170 - acc: 0.9942 - val_loss: 0.0212 - val_acc: 0.9930\n",
      "Epoch 192/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.0159 - acc: 0.9946 - val_loss: 0.0214 - val_acc: 0.9940\n",
      "Epoch 193/200\n",
      "48000/48000 [==============================] - 391s - loss: 0.0164 - acc: 0.9942 - val_loss: 0.0242 - val_acc: 0.9920\n",
      "Epoch 194/200\n",
      "48000/48000 [==============================] - 401s - loss: 0.0151 - acc: 0.9949 - val_loss: 0.0212 - val_acc: 0.9930\n",
      "Epoch 195/200\n",
      "48000/48000 [==============================] - 391s - loss: 0.0160 - acc: 0.9942 - val_loss: 0.0241 - val_acc: 0.9900\n",
      "Epoch 196/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0146 - acc: 0.9945 - val_loss: 0.0203 - val_acc: 0.9940\n",
      "Epoch 197/200\n",
      "48000/48000 [==============================] - 392s - loss: 0.0145 - acc: 0.9952 - val_loss: 0.0221 - val_acc: 0.9930\n",
      "Epoch 198/200\n",
      "48000/48000 [==============================] - 387s - loss: 0.0166 - acc: 0.9938 - val_loss: 0.0302 - val_acc: 0.9890\n",
      "Epoch 199/200\n",
      "48000/48000 [==============================] - 388s - loss: 0.0168 - acc: 0.9945 - val_loss: 0.0216 - val_acc: 0.9930\n",
      "Epoch 200/200\n",
      "48000/48000 [==============================] - 412s - loss: 0.0159 - acc: 0.9945 - val_loss: 0.0218 - val_acc: 0.9940\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x7f182815ba50>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_X, train_Y, batch_size=batch_size, epochs=epochs,\n",
    "          verbose=1, validation_data=(test_X, test_Y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('Test loss:', 0.021821705218362696)\n",
      "('Test accuracy:', 0.99399999999999999)\n"
     ]
    }
   ],
   "source": [
    "score = model.evaluate(test_X, test_Y, verbose=0)\n",
    "print('Test loss:', score[0])\n",
    "print('Test accuracy:', score[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model.save('./zheyeV5.keras')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
